package org.xhnews.download;

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.HttpConnection;

import org.xhnews.log.RaspberryLogger;
import org.xhnews.preferences.Preferences;
import org.xhnews.utils.FileUtil;
import org.xhnews.utils.NetworkUtil;

import net.rim.device.api.io.transport.ConnectionDescriptor;
import net.rim.device.api.io.transport.ConnectionFactory;

public class Download {
	private static ConnectionFactory connFact = NetworkUtil.getConnectionFactory();
	/*
	 * 发送HTTP请求获得数据
	 */
	public static InputStream get(String url) {
		ConnectionDescriptor connDesc = connFact.getConnection(url);
		if (connDesc != null) {
			HttpConnection httpConn = (HttpConnection) connDesc.getConnection();
			try {
				return httpConn.openInputStream();
			} catch (IOException e) {
				RaspberryLogger.getInstance().info(e.getMessage());
				e.printStackTrace();
			}
		}
		return null;
	}
	public static void getAndSaveASFile(String url,String path,String filename){
		String fileurl=url+filename;
		RaspberryLogger.getInstance().info(fileurl);
		ConnectionDescriptor connDesc = connFact.getConnection(fileurl);
		if (connDesc != null) {
			HttpConnection httpConn = (HttpConnection) connDesc.getConnection();
			try {
				InputStream is= httpConn.openInputStream();
				FileUtil.writeFile(path, filename, is);
			} catch (IOException e) {
				RaspberryLogger.getInstance().info(e.getMessage());
				e.printStackTrace();
			}
		}
	}
	
	
}
